﻿Imports System.IO

Namespace Algorithm

    Public MustInherit Class Image

        Public Shared Function Scale(ByVal originalImg As Graphics.Texture2D, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Graphics.Texture2D
            Using ms As New MemoryStream()
                originalImg.SaveAsPng(ms, originalImg.Width, originalImg.Height)
                Return Texture2D.FromStream(originalImg.GraphicsDevice, ms, width, height, aspectRatio)
                ms.Close()
            End Using
        End Function

        Public Shared Function Scale(ByVal ifs As IsolatedStorage.IsolatedStorageFile, ByVal originalImg As String, ByVal newFileName As String, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Boolean
            Try
                Using sourceStream = ifs.OpenFile(originalImg, FileMode.Open)
                    Dim oriImageObject = Texture2D.FromStream(Nothing, sourceStream, width, height, aspectRatio)
                    Using targetStream = ifs.OpenFile(newFileName, FileMode.Create)
                        oriImageObject.SaveAsPng(targetStream, width, height)
                        targetStream.Close()
                    End Using
                    sourceStream.Close()
                End Using
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

    End Class

End Namespace